package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.MainApplication;
import com.teach.javafxclient.util.HttpUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;

import java.io.IOException;

/**
 * @author Liang Jianlin
 * @date 2024-04-30 02:13
 */
public class CourseAnnouncementController {
    @FXML
    public HBox addAnnouncement;
    @FXML
    public VBox itemContainer;
    @FXML
    public VBox addContainer;
    public VBox pinContainer;

    public void setAnnouncement(String id) throws IOException {
        JSONArray res = HttpUtil.get(Globalvar.baseUrl + "/api/courses/" + id + "/notices")
                .asJson().getBody().getArray();
        for (int i = 0; i < res.length(); i++) {
            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/announcement-item.fxml"));
            Parent root = fxmlLoader.load();
            AnnouncementItemController AIC = fxmlLoader.getController();
            AIC.setItem((String)((JSONObject)res.get(i)).get("title"),(String)((JSONObject)res.get(i)).get("content"),((String)((JSONObject)res.get(i)).get("sendTime")).substring(0,10),(Boolean) ((JSONObject)res.get(i)).get("pinned"),String.valueOf(((JSONObject)res.get(i)).get("id")));
            int finalI = i;
            AIC.pinIt.setOnMouseClicked(e->{
                System.out.println("pin");
                HttpResponse resPin = HttpUtil.post(Globalvar.baseUrl + "/api/notices/pin")
                        .header("Content-Type", "application/json")
                        .body(((JSONObject)res.get(finalI)).get("id"))
                        .asJson();
                if(resPin.getStatus() == 200){
                    System.out.println("pin ok");
                    itemContainer.getChildren().remove(root);
                    AIC.setItem((String)((JSONObject)res.get(finalI)).get("title"),(String)((JSONObject)res.get(finalI)).get("content"),((String)((JSONObject)res.get(finalI)).get("sendTime")).substring(0,10),true,String.valueOf(((JSONObject)res.get(finalI)).get("id")));
                    pinContainer.getChildren().add(root);
                }
                e.consume();
            });
            AIC.deletePin.setOnMouseClicked(e->{
                System.out.println("delete");
                String noticeId = String.valueOf(((JSONObject)res.get(finalI)).get("id"));
                HttpResponse resPin = HttpUtil.delete(Globalvar.baseUrl + "/api/notices/pin/"+noticeId)
                        .header("Content-Type", "application/json")
                        .asJson();
                System.out.println(resPin.getStatus());
                if(resPin.getStatus() == 200){
                    System.out.println("delete ok");
                    pinContainer.getChildren().remove(root);
                    AIC.setItem((String)((JSONObject)res.get(finalI)).get("title"),(String)((JSONObject)res.get(finalI)).get("content"),((String)((JSONObject)res.get(finalI)).get("sendTime")).substring(0,10),false,String.valueOf(((JSONObject)res.get(finalI)).get("id")));
                    itemContainer.getChildren().add(root);
                }
                e.consume();
            });
            if((Boolean) ((JSONObject)res.get(i)).get("pinned")){
                pinContainer.getChildren().add(root);
            }else {
                itemContainer.getChildren().add(root);
            }
        }
    }
    //FIXME:等待接口完成方法
    public void setGlobalAnnouncement() throws IOException {
        JSONArray res = HttpUtil.get(Globalvar.baseUrl+"/api/notices/public")//等接口
                .asJson().getBody().getArray();
        for (int i = 0; i < res.length(); i++) {
            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/announcement-item.fxml"));
            Parent root = fxmlLoader.load();
            AnnouncementItemController AIC = fxmlLoader.getController();
            AIC.setItem((String)((JSONObject)res.get(i)).get("title"),(String)((JSONObject)res.get(i)).get("content"),((String)((JSONObject)res.get(i)).get("sendTime")).substring(0,10),(Boolean) ((JSONObject)res.get(i)).get("pinned"),String.valueOf(((JSONObject)res.get(i)).get("id")));
            int finalI = i;
            AIC.pinIt.setOnMouseClicked(e->{
                System.out.println("pin");
                HttpResponse resPin = HttpUtil.post(Globalvar.baseUrl+"/api/notices/public")
                        .header("Content-Type", "application/json")
                        .body(((JSONObject)res.get(finalI)).get("id"))
                        .asJson();
                if(resPin.getStatus() == 200){
                    System.out.println("pin ok");
                    itemContainer.getChildren().remove(root);
                    AIC.setItem((String)((JSONObject)res.get(finalI)).get("title"),(String)((JSONObject)res.get(finalI)).get("content"),((String)((JSONObject)res.get(finalI)).get("sendTime")).substring(0,10),true,String.valueOf(((JSONObject)res.get(finalI)).get("id")));
                    pinContainer.getChildren().add(root);
                }
                e.consume();
            });
            AIC.deletePin.setOnMouseClicked(e->{
                System.out.println("delete");
                String noticeId = String.valueOf(((JSONObject)res.get(finalI)).get("id"));
                HttpResponse resPin = HttpUtil.delete(Globalvar.baseUrl+"/api/notices/public"+noticeId)
                        .header("Content-Type", "application/json")
                        .asJson();
                System.out.println(resPin.getStatus());
                if(resPin.getStatus() == 200){
                    System.out.println("delete ok");
                    pinContainer.getChildren().remove(root);
                    AIC.setItem((String)((JSONObject)res.get(finalI)).get("title"),(String)((JSONObject)res.get(finalI)).get("content"),((String)((JSONObject)res.get(finalI)).get("sendTime")).substring(0,10),false,String.valueOf(((JSONObject)res.get(finalI)).get("id")));
                    itemContainer.getChildren().add(root);
                }
                e.consume();
            });
            if((Boolean) ((JSONObject)res.get(i)).get("pinned")){
                pinContainer.getChildren().add(root);
            }else {
                itemContainer.getChildren().add(root);
            }
        }
    }
}
